package com.mingchuang.administrator.mingchuang.xml;

import android.util.Log;
import android.util.Xml;

import com.mingchuang.administrator.mingchuang.bean.PileInfoBean;
import com.mingchuang.administrator.mingchuang.utils.Util;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Xml 解析    解析完可以直接存入数据库中
 */
public class PileXml {

    //读取XML
    public static PileInfoBean readXML(InputStream inStream) {
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(inStream, "UTF-8");
            int eventType = parser.getEventType();
            //eventType = parser.next();

            PileInfoBean pileInfo = null;
            String selection = "";

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT://文档开始事件,可以进行数据初始化处理
                        pileInfo = new PileInfoBean();

                    case XmlPullParser.START_TAG://开始元素事件
                        String name = parser.getName();
                        Log.d("kipeng", "name:" + parser.getName());
                        //Log.d("kipeng","value:"+safeNextText(parser));

                        //读取数据
                        if(!Util.checkIsNull(name)){
                            switch (name) {
                                case "proname":
                                    pileInfo.setProjectName(parser.nextText());

                                    break;
                                case "pilename":
                                    pileInfo.setPileName(parser.nextText());
                                    break;
                                case "testlen":
                                    pileInfo.setSectionNum(parser.nextText());
                                    break;
                                case "sectionnum":
                                    pileInfo.setTestLen(parser.nextText());
                                    break;
                                case "start":
                                    selection = selection + parser.nextText()+"-";
                                    break;
                                case "end":
                                    selection = selection + parser.nextText()+":";
                                    break;
                                case "testdate":
                                    pileInfo.setTestDate(parser.nextText());
                                    break;
                                case "uploaddate":
                                    pileInfo.setUploadDate(parser.nextText());
                                    break;
                                default:
                                    break;



                        }


                    }


                    break;
                    case XmlPullParser.END_TAG://结束元素事件
                        Log.d("kipeng", "end name:" + parser.getName());
                        pileInfo.setSection(selection);
                        break;
                }
                eventType = parser.next();
            }
            inStream.close();

            Log.d("kipeng", pileInfo.toString());
            return pileInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    //读取XML
    public static PileInfoBean readXML(File file) {
        InputStream inStream = null;
        try {
            inStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(inStream, "UTF-8");
            int eventType = parser.getEventType();
            //eventType = parser.next();

            PileInfoBean pileInfo = null;
            String selection = "";

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT://文档开始事件,可以进行数据初始化处理
                        pileInfo = new PileInfoBean();

                    case XmlPullParser.START_TAG://开始元素事件
                        String name = parser.getName();
                        Log.d("kipeng", "name:" + parser.getName());
                        //Log.d("kipeng","value:"+safeNextText(parser));

                        //读取数据
                        if(!Util.checkIsNull(name)){
                            switch (name) {
                                case "proname":
                                    pileInfo.setProjectName(parser.nextText());

                                    break;
                                case "pilename":
                                    pileInfo.setPileName(parser.nextText());
                                    break;
                                case "testlen":
                                    pileInfo.setTestLen(parser.nextText());
                                    break;
                                case "sectionnum":
                                    pileInfo.setSectionNum(parser.nextText());
                                    break;
                                case "start":
                                    selection = selection + parser.nextText()+"-";
                                    break;
                                case "end":
                                    selection = selection + parser.nextText()+":";
                                    break;
                                case "testdate":
                                    pileInfo.setTestDate(parser.nextText());
                                    break;
                                case "uploaddate":
                                    pileInfo.setUploadDate(parser.nextText());
                                    break;
                                default:
                                    break;



                            }


                        }


                        break;
                    case XmlPullParser.END_TAG://结束元素事件
                        Log.d("kipeng", "end name:" + parser.getName());
                        pileInfo.setSection(selection);
                        break;
                }
                eventType = parser.next();
            }
            inStream.close();

            Log.d("kipeng", pileInfo.toString());
            return pileInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static boolean createXml(String path, String x, String y){

        OutputStream os = null;
        try {
            os = new FileOutputStream(path);
            XmlSerializer xml = Xml.newSerializer();

            xml.setOutput(os,"UTF-8");
            xml.startDocument("UTF-8",true);
            xml.startTag(null,"pile_gps");
            xml.startTag(null,"local_x");
            xml.text(x);

            xml.endTag(null,"local_x");
            xml.startTag(null,"local_y");
            xml.text(y);


            xml.endTag(null,"local_y");

            xml.endTag(null,"pile_gps");
            xml.endDocument();
            xml.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }





        return true;

    }




}
